package samsung.sds.survey.surveyManagement.service.impl;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import samsung.sds.survey.surveyManagement.dao.SurveyDesignMapper;
import samsung.sds.survey.surveyManagement.dao.SurveyManagementMapper;
import samsung.sds.survey.surveyManagement.model.QuestionEntity;
import samsung.sds.survey.surveyManagement.model.QuestionModel;
import samsung.sds.survey.surveyManagement.model.SurveyEntity;
import samsung.sds.survey.surveyManagement.service.SurveyDesignService;
import samsung.sds.survey.surveyManagement.service.SurveyManagementService;
import samsung.sds.system.common.baseQuery.AbstractBaseService;
import samsung.sds.system.common.baseQuery.BaseMapper;
import samsung.sds.system.utils.Constants;

/**
 * @author deng1.pan
 * @createDate 2016-01-08
 * 
 *             问卷管理模块的serviceImpl
 */
@Service
public class SurveyManagementServiceImpl extends
		AbstractBaseService<SurveyEntity> implements SurveyManagementService {
	@Autowired
	private SurveyManagementMapper surveyManagementMapper;

	@Autowired
	private SurveyDesignMapper surveyDesignMapper;

	@Autowired
	private SurveyDesignService surveyDesignServiceImpl;

	/**
	 * 重写获取mapper的方法
	 */
	@Override
	protected BaseMapper<SurveyEntity> getMapper() {
		return surveyManagementMapper;
	}

	/**
	 * 插入实体
	 * 
	 * @param survey
	 * @return 成功返回1
	 */
	public int insertEntity(SurveyEntity survey) {
		return this.getMapper().insertEntity(survey);
	}

	/**
	 * 更新实体
	 * 
	 * @param survey
	 * @return 成功返回1
	 */
	public int updateEntity(SurveyEntity survey) {
		return this.getMapper().updateEntity(survey);
	}

	/**
	 * 复制插入新调查问卷问题
	 * 
	 * @param surveyIdOld
	 * @param surveyIdNew
	 * @param create_User
	 * @return 成功返回true
	 */
	@Override
	public boolean copyQuestion(String surveyIdOld, String surveyIdNew,
			String create_User) {
		boolean flag = true;
		List<QuestionEntity> qlist = surveyDesignServiceImpl.getQuestionEntity(surveyIdOld);
		if (qlist.size() > 0) {
			QuestionModel qm = new QuestionModel();
			qm.setQuestionList(qlist);
			System.out.println(qm);
			SurveyEntity surveyEntity = surveyManagementMapper.selectTF_F_SURVEY(surveyIdNew);
			flag = surveyDesignServiceImpl.saveQuestion(surveyEntity, qm);
		}
		return flag;

	}

	/**
	 * 停止投票
	 * 
	 * @param survey
	 * @return
	 */
	public int stopVote(SurveyEntity survey) {
		return surveyManagementMapper.updateTF_F_SURVEY01(survey);
	}

	/**
	 * 定时更新问卷状态
	 * 
	 * @param status
	 */
	public void updateSurveyStatusTask(String status) {
		surveyManagementMapper.updateTF_F_SURVEY02(status);
	}

	/**
	 * 删除包含question的问卷
	 * 
	 * @param surveyId
	 */
	public String deleteSurvey(String id) {
		// 检查参数
		if (id == null) {
			return Constants.ERRMSG_PARAM_ERROR;
		}
		// 获取question集合
		List<QuestionEntity> list = surveyDesignServiceImpl.getQuestionEntity(new String(id));
		// 删除问题选项
		for (QuestionEntity que : list) {
			surveyDesignServiceImpl.deleteOption(que.getId());
		}
		// 删除问题
		SurveyEntity survey = new SurveyEntity();
		survey.setSurvey_Id(new String(id));
		surveyDesignServiceImpl.deleteEntity(survey);
		// 删除问卷
		int rlt = this.deleteEntityById(id);
		if (rlt == 1) {
			return Constants.SUCCESS_CODE;
		} else {
			return Constants.FAILURE_CODE;
		}
	}

	/**
	 * 根据调查ID获取调查信息
	 * 
	 * @param survey_Id
	 * @return
	 */
	public SurveyEntity getSurveyInfo(String survey_Id) {
		return surveyManagementMapper.selectTF_F_SURVEY(survey_Id);
	}

}
